//初始化控件
//$(selector).combotree(options);
$.fn.combotree = function (options) {
    // ie8及其以下版本不支持foreach，次方法用来让不支持foreach的浏览器支持foreach
    if (typeof Array.prototype.forEach != 'function') {
        Array.prototype.forEach = function (callback) {
            for (var i = 0; i < this.length; i++) {
                callback.apply(this, [this[i], i, this]);
            }
        };
    }
    // 为插件定义一些默认参数
    arguments.callee.defaults = {     //argument.callee  指向当前调用的 this
        elementClick: function (nodes, tree) {
        },   //点击节点后触发的函数
        clearSelClick: function () {
        },   // 情况输入框的值
        isExtend: false,   // 是否把下拉树全部展开
        disabled: false,   // 是否禁用
        maxHeight: 250,   // 下拉树div的高度
        displayParentVal: false    // 是否禁用父节点
    };
    options = $.extend(true, {}, {"defaults": $.fn.combotree.defaults}, options || {});    // $.extend 可以把参数合并,前面会覆盖前面
    console.log(options)
    var treeId = $(this).attr("id");

    if ($("#" + treeId + "_parentName").length < 1) {   // 控制重新设置url时不会出现渲染两次的错误
        posCon = $('<div style="position:relative;"></div>');
        target = $('<input readonly="readonly"  id="' + treeId + '_parentName" name="' + options.input_name + '" type="text" class="form-control ' + treeId + '_treeDrop" value="" placeholder="请选择"/>');
        $(this).before(posCon);
        posCon.append(target);
        dwidth = $("#" + treeId + "_parentName").outerWidth() - 2;
        target.after('<i id="' + treeId + '_treeDrop" class="fa fa-chevron-down ' + treeId + '_treeDrop" style="position:absolute;right:8px;top:8px;cursor:pointer;z-index:9;"></i>')
        treeDemo = $("<div id='" + treeId + "_wrap' style='overflow-x:hidden;overflow-y:auto;z-index:19;line-height: 1;top:34px;background-color:white;display:none;border: 1px solid lightgrey;width: " + dwidth + "px;max-height: " + options.defaults.maxHeight + "px;position:absolute;'></div>");
        searchbox = $("<div id='" + treeId + "_searchbox' class='input-group' style='width:" + (dwidth - 22) + "px;margin:5px auto;'><span class='input-group-addon'><i class='fa fa-search'></i></span><input id='" + treeId + "_search_ay' autocomplete='off' type='text' placeholder='搜索' class='form-control'/><span class='input-group-btn'><button id='" + treeId + "_clear' type='button' class='btn btn-default' style='border:0;border-top:1px solid #ccc;border-bottom:1px solid #ccc;'><i class='fa fa-times-circle-o'></i></button></span><span class='input-group-btn'><button id='" + treeId + "_clearSel' type='button' class='btn btn-default'>取消选中</button></span></div>")
        treeDemo1 = $("<div   id='" + treeId + "_mytree' style='width: 100%;height: 200px;'></div>");
        target.after(treeDemo);
        //  treeDemo.append(searchbox);
        treeDemo.append(treeDemo1);
    } else {
        $("." + treeId + "_mytree").empty();
        $("." + treeId + "_mytree").remove();
        target = $("#" + treeId + "_parentName");
        treeDemo = $("#" + treeId + "_wrap");
        searchbox = $("#" + treeId + "_searchbox");
        treeDemo1 = $("<div class='" + treeId + "_mytree' style='width: 100%;height: 220px;'></div>");
        treeDemo.append(treeDemo1);
    }
    // if (options.value) {
    //     $("#" + treeId + '_parentName').val(options.value);
    // }
    //加载数据
    getAjax(options.get_url,{}, function (data) {
        console.log(data)
        $('#' + treeId + '_mytree').jstree({
            'core': {
                'data': data
            },
            "plugins": ["search"]
        });
    });
    $("." + treeId + "_treeDrop").click(function () {
        if ($("#" + treeId + "_wrap").css("display") == 'none') {//如果show是隐藏的
            $("#" + treeId + "_wrap").css("display", "block");//show的display属性设置为block（显示）
        } else {
            //如果show是显示的
            $("#" + treeId + "_wrap").css("display", "none");//show的display属性设置为none（隐藏）
        }
    })
    $('#' + treeId + '_mytree').on("changed.jstree", function (e, data) {
        if (data.selected != -1) {
            var deptId = data.selected[0];
            var deptName = data.instance.get_node(data.selected[0]).text;
            $("#" + treeId).val(deptId);
            $("#" + treeId + '_parentName').val(deptName);
            $("#" + treeId + "_wrap").css("display", "none");
        }
    });
    $('#' + treeId + '_mytree').on("loaded.jstree", function (event, data) {
        if (options.value) {
            data.instance.select_node(options.value);
        }
    });
}

